<template>
  <div class="app-container">

    <!--搜索表单-->
    <el-form :inline="true" :model="searchEmp" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input
          v-model="searchEmp.name"
          placeholder="请输入员工姓名"
        />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="searchEmp.gender" placeholder="请选择">
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="入职时间">
        <el-date-picker
          v-model="entrydate"
          clearable
          value-format="yyyy-MM-dd"
          type="daterange"
          placeholder="选择日期"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 400px; margin-left: 20px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="info" @click="clear">清空</el-button>
      </el-form-item>
    </el-form>

    <br>
    <!--表格-->
    <template>
      <el-table :data="tableData" style="width: 100%" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column prop="name" label="姓名" align="center" />
        <el-table-column prop="nickName" label="昵称" align="center" />

        <el-table-column align="center" label="性别">
          <template slot-scope="scope">
            <span style="margin-right: 10px">
              {{ scope.row.gender == "1" ? "男" : "女" }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="phone" label="联系电话" align="center" />

        <el-table-column prop="score" label="积分" align="center" />


        <el-table-column align="center" label="注册时间">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>

      </el-table>
    </template>

    <br>

    <!--分页工具条-->
    <el-pagination
      :background="background"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { add, update, deleteById, selectById, findAll, loadFiles } from '@/api/emp.js'
import { page } from '@/api/user.js'
// import { findAll } from '@/api/dept.js'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      background: true,

      /** 添加数据对话框是否展示的标记 */
      dialogVisible: false,


      /** 被选中的id数组 */
      selectedIds: [],
      /** 复选框选中数据集合 */
      multipleSelection: [],

      /** 表格数据 */
      tableData: [],

      token: { token: getToken() },

      /** 图片上传的后端接口，但是这个是不能跨域的，默认后端解决了跨域问题，后面用request优化 */
      upLoadUrl: process.env.VUE_APP_URL + "common/upload",

      ///////// 分页查询相关 ////////
      searchEmp: {
        name: '',
        gender: ''
      },
      beginTime: '',
      endTime: '',
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      entrydate: ''
    }
  },

  watch: {
    entrydate(val) {
      if (val && val.length >= 2) {
        this.beginTime = val[0]
        this.endTime = val[1]
      } else {
        this.beginTime = ''
        this.endTime = ''
      }
    }
  },

  mounted() { // 钩子方法
    this.page() // 当页面加载完成后，发送异步请求，获取数据
  },

  methods: {
    /**
     * 查询分页数据
     */
    page() {
      page( // 向后端发请求
        this.searchEmp.name,
        this.searchEmp.gender,
        this.beginTime,
        this.endTime,
        this.currentPage,
        this.pageSize
      ).then((res) => {
        // 响应数据的data里面有total和records，去看接口文档
        this.totalCount = res.data.data.total
        this.tableData = res.data.data.records
      })
    },
    // 查询方法
    onSubmit() {
      this.currentPage = 1
      this.page()
    },
   // 清空
    clear() {
      this.searchEmp = { name: '', gender: '' }
      this.beginTime = ''
      this.endTime = ''
      this.entrydate = ''
      this.page()
    },

    // 复选框选中后执行的方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    /**
     * 分页

     * @param {Object} val
     */
    handleSizeChange(val) {
      // 重新设置每页显示的条数
      this.pageSize = val
      this.page()
    },

    handleCurrentChange(val) {
      // 重新设置当前页码
      this.currentPage = val
      this.page()
    }
  }
}
</script>
<style>
/* .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
} */

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
